package com.lgit.Model4.hw04;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SeverTest {

    public static void main(String[] args) {
        ServerSocket ss = null;
        Socket acc = null;
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        try {
            // 1.创建ServerSocket类型的对象并提供端口号
            ss = new ServerSocket(8686);

            // 2.调用accept方法，等待客户端的连接请求
            System.out.println("等待客户端连接...");
            acc = ss.accept();
            System.out.println("客户端连接成功...");

            // 3.使用输入输出流进行通信
            ois = new ObjectInputStream(acc.getInputStream());
            oos = new ObjectOutputStream(acc.getOutputStream());
            try {
                // 读取到客户端发送的UserMessage对象
                UserMessage receiveUm = (UserMessage) ois.readObject();
                System.out.println("接收客户端数据成功");
                System.out.println("判断前UserMessage对象中的类型为: " + receiveUm.getType());
                System.out.println("正在核对用户名及密码，请稍后...");
                Thread.sleep(6000);

                // 根据题目要求，对User类中的用户名及密码进行判断
                if ("admin".equals(receiveUm.getUser().getName()) && "123456".equals(receiveUm.getUser().getPassword())) {
                    receiveUm.setType("success"); // 同时满足条件，UserMessage类型改为success
                } else {
                    receiveUm.setType("fail"); // 不满足条件，UserMessage类型改为fail
                }

                //System.out.println("admin".equals(um.getUser().getName()) && "123456".equals(um.getUser().getPassword()));
                System.out.println("判断后UserMessage对象中的类型为: " + receiveUm.getType());
                System.out.println("正在将UserMessage对象发送给客户端，请稍后...");
                Thread.sleep(6000);

                // 将判断并修改类型后的UserMessage对象发送到客户端做进一步判断
                oos.writeObject(receiveUm);
                System.out.println("发生对象数据成功");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 4.关闭Socket和输入输出流释放相关资源
            if (null != acc) {
                try {
                    acc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != ss) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != ois) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null != oos) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
